package online.longzipeng.householdbillssystem.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import online.longzipeng.householdbillssystem.business.entity.BillGroup;
import online.longzipeng.householdbillssystem.business.vo.bill.BillGroupSaveVO;
import online.longzipeng.householdbillssystem.business.vo.bill.BillGroupUpdateVO;
import online.longzipeng.householdbillssystem.common.result.ResponseEnum;
import online.longzipeng.householdbillssystem.common.result.Result;
import online.longzipeng.householdbillssystem.common.utils.ExceptionUtils;
import online.longzipeng.householdbillssystem.service.BillGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/bill_group")
@Api(value = "用户组中的账单相关接口",description = "用户组中的账单相关接口",tags = "用户组中的账单相关接口")
@Slf4j
public class BillGroupController {

    @Autowired
    BillGroupService billGroupService;

    @PostMapping("/save")
    @ApiOperation("把账单添加到用户组中")
    public Result saveToGroup(@Valid @RequestBody BillGroupSaveVO billGroupSaveVO){
        BillGroup billGroup = new BillGroup();
        BeanUtils.copyProperties(billGroupSaveVO,billGroup);
        ExceptionUtils.isTrue(billGroupService.add(billGroup),"账单添加到用户组失败！！请检查填写的信息",billGroup);
        if (log.isDebugEnabled()){
            log.info(String.format("账单添加到用户组成功，信息：%s",billGroup.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/delete_one/{id}")
    @ApiOperation("删除用户组中指定id的账单")
    public Result deleteOneFromGroup(@ApiParam(value = "账单用户组关联表的id", required = true) @PathVariable("id") Integer id){
        ExceptionUtils.isTrue(billGroupService.delete(id),"删除失败！可能是id不存在或被别人删除",id);
        if (log.isDebugEnabled()){
            log.info(String.format("删除成功，id为：%s",id));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除用户组中多个账单")
    public Result deleteFromGroup(@ApiParam(value="账单用户组关联表的id数组",required = true, example = "1,2,3,4") @RequestParam("ids") Integer[] ids){
        for (Integer id : ids) {
            ExceptionUtils.isTrue(billGroupService.delete(id),"删除失败！！参数不合法或已被其他用户删除",id);
        }
        if (log.isDebugEnabled()){
            log.info(String.format("账单批量删除成功，ids：%s",ids.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

    @PutMapping("/update")
    @ApiOperation("修改用户组中的账单")
    public Result updateToGroup(@Valid @RequestBody BillGroupUpdateVO billGroupUpdateVO){
        BillGroup billGroup = new BillGroup();
        BeanUtils.copyProperties(billGroupUpdateVO,billGroup);
        ExceptionUtils.isTrue(billGroupService.edit(billGroup),"用户组中的账单修改失败！！请检查填写的信息",billGroup);
        if (log.isDebugEnabled()){
            log.info(String.format("用户组中的账单修改成功，信息：%s",billGroup.toString()));
        }
        return new Result(ResponseEnum.SUCCESS);
    }

}